﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.SoftReferences;
using Microsoft.Xna.Framework;
using GameObjects;

namespace WindowsGame.Ui
{
    class UiUnit : VisibleObject
    {
        public UiHex UiHex { get; set; }
        public SoftRef<Unit> Unit { get; set; }

        public UiUnit(UiModel uiModel) : base(uiModel)
        {
            uiModel.UiUnits.Add(this);
        }

        public override float Alpha
        {
            get
            {
                if (UiModel.UnitSelection != null && UiModel.UnitSelection.UiUnit != this)
                    return .5f;
                return 1f;
            }
        }

        public override Vector3 DiffuseColor
        {
            get
            {
                return ColourHelper.FromHexString(Unit.Target.Color);
            }
        }

        public void Update(GameTime gameTime, int units, int unitNumber)
        {
            WorldTransform = GetWorldTransform(UiHex, units, unitNumber);
            
            base.Update(gameTime);
        }

        public static Matrix GetWorldTransform(UiHex uiHex, int units, int unitNumber)
        {
            //var numberOfUnits = uiHex.Hex.Target.Units.Count();

            var pointRatio = (float)unitNumber / (uiHex.Hex.Target.StackLimit * 2);
            if (units > uiHex.Hex.Target.StackLimit * 2)
                pointRatio = (float)unitNumber / units;
            var xStep = Math.Cos(pointRatio * 2 * Math.PI);
            var yStep = Math.Sin(pointRatio * 2 * Math.PI);
            var pointX = (float)xStep * .6f;
            var pointY = (float)yStep * .6f;

            return Matrix.CreateTranslation(pointX, pointY, 0.15f) * uiHex.GetWorldTransform();
        }
    }
}
